package com.quasar.hpatient.utils;

import android.text.TextUtils;

import com.vise.log.ViseLog;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * author : 许建宏
 * Date: 2017/11/4 0004
 * View:
 */

public class OutliersValues {

    //危险下线
    public static final int LOWNORMALVALUE = -1;
    //正常
    public static final int NORMALVALUE = 0;
    //危险上限
    public static final int UPNORMALVALUE = 1;
    //危险
    public static final int DANGERLVALUE = 2;
    //边界
    public static final int BOUNDARYVALUE = 3;
    //异常
    public static final int ABNORMAL = -1;

    public static final int BIGER = 1;

    public static int detection(String reference_value, double Testing) {

        // 正常
        int stats = 0;

        double maxValues = Double.MAX_VALUE;

        double minValues = -1;

        if (reference_value.contains("-")) {

            String[] reference = reference_value.split("-");

            if (UrlUtils.StrNum(reference[0]) && UrlUtils.StrNum(reference[1])) {

                minValues = Double.parseDouble(reference[0]);
                maxValues = Double.parseDouble(reference[1]);

            }


        } else if (reference_value.contains("~")) {

            String[] reference = reference_value.split("~");

            if (UrlUtils.StrNum(reference[0]) && UrlUtils.StrNum(reference[1])) {

                minValues = Double.parseDouble(reference[0]);
                maxValues = Double.parseDouble(reference[1]);


            }

        } else if (reference_value.contains("^"))

        {

            String[] reference = reference_value.split("^");

            if (UrlUtils.StrNum(reference[0]) && UrlUtils.StrNum(reference[1])) {

                minValues = Double.parseDouble(reference[0]);
                maxValues = Double.parseDouble(reference[1]);


            }

        } else if (reference_value.contains("<")) {

            String[] reference = reference_value.split("<");


            if (UrlUtils.StrNum(reference[1])) {

                maxValues = Double.parseDouble(reference[1]);


            }


        } else if (reference_value.contains("<="))

        {

            String[] reference = reference_value.split("<=");
            if (UrlUtils.StrNum(reference[1])) {
                maxValues = Double.parseDouble(reference[1]) + 0.00001;
            }


        } else if (reference_value.contains(">"))

        {


            String[] reference = reference_value.split(">");
            if (UrlUtils.StrNum(reference[1])) {
                minValues = Double.parseDouble(reference[1]);

            }

        } else if (reference_value.contains(">="))

        {

            String[] reference = reference_value.split(">=");
            if (UrlUtils.StrNum(reference[1])) {
                minValues = Double.parseDouble(reference[1]) - 0.00001;

            }

        }


        ViseLog.d("检测值和参考值"+Testing+"---"+minValues);
//        if (Testing <= minValues) {
//            stats = ABNORMAL;
//        } else if (Testing >= maxValues) {
//            stats = BIGER;
//        }
        if (Testing < minValues) {
            stats = ABNORMAL;
        } else if (Testing > maxValues) {
            stats = BIGER;
        }else if(Testing == minValues){
            stats=0;
        }else if(Testing > maxValues){
            stats=0;
        }


        return stats;
    }


    //根据输入的值判断值的状态以及颜色
    public static int detectionValues(String Standard, double testValues) {


        int returnValue = 0;
        if (Standard != null) {
            returnValue = detection(Standard, testValues);
        }
        return returnValue;
    }


    public static int TestingDetectionValues(String Standard, double testValues,String boundary,int Identification) {

        int returnValue = 0;


        if (Standard != null) {


            // TODO: 2018/3/23
//            if(TextUtils.isEmpty(boundary)){
//
//                try {
//
//                    String[] split = Standard.split("-");
//
//                    if(null != split && split.length == 2){
//
//                        Standard = Float.parseFloat(split[0])/10+"-"+Float.parseFloat(split[0])*10;
//                    }
//
//                }catch (Exception e){
//
//                }
//
//            }


            returnValue = detection(Standard, testValues);
            if(boundary!=null && boundary.length()>0){
                String boundaryVal=Testingdetection(boundary,Identification);
                int stats=detection(boundaryVal,testValues);
                if(stats==1){
                    ViseLog.d("不可能值"+boundaryVal+"***"+stats);
                    returnValue=BOUNDARYVALUE;
                }else if(stats==-1){
                    ViseLog.d("不可能值"+boundaryVal+"***"+stats+"状态"+-1);
                    returnValue=-1;
                }
            }

        }

        return returnValue;
    }


    /**
     * 化验单检测边界值
     * @param detectionValue 检测值
     * @param reference_value 参考值
     * @return
     * stats  标识  区别边界值
     */
    // TODO: 2018/3/23
    public static int TestingTextColor(String detectionValue, String reference_value,String boundary,int Identification) {
        int result = 0;

        ViseLog.d("参考值++++"+reference_value);
        //检测值
        double Testing = getMsg(detectionValue);
        List<String> Strlist = new ArrayList<String>();

        if (reference_value != null && reference_value.length() > 0) {      //参考值
            Strlist.add(reference_value);
        }

        int stats = TestingDetectionValues(reference_value, Testing,boundary,Identification);//Testing


        switch (stats) {
            //正常值
            case NORMALVALUE:
                result = NORMALVALUE;
                break;
            case UPNORMALVALUE:
                result = UPNORMALVALUE;
                break;

            case LOWNORMALVALUE:
                result = LOWNORMALVALUE;
                break;


            case DANGERLVALUE:
                result = DANGERLVALUE;
                break;

            case BOUNDARYVALUE:
                result = BOUNDARYVALUE;
                break;
        }
        return result;
    }


    /**
     * 根据检测值改变字体颜色
     * detectionValue 检测值
     *
     * @param reference_value 参考值
     */
    public static int TextColor(String detectionValue, String reference_value) {
        int result = 0;
        //检测值
        double Testing = getMsg(detectionValue);
        List<String> Strlist = new ArrayList<String>();

        if (reference_value != null && reference_value.length() > 0) {      //参考值
            Strlist.add(reference_value);
        }

        int stats = detectionValues(reference_value, Testing);//Testing

        switch (stats) {
            //正常值
            case NORMALVALUE:
                result = NORMALVALUE;
                break;
            case UPNORMALVALUE:
                result = UPNORMALVALUE;
                break;

            case LOWNORMALVALUE:
                result = LOWNORMALVALUE;
                break;


            case DANGERLVALUE:
                result = DANGERLVALUE;
                break;

            case BOUNDARYVALUE:
                result = BOUNDARYVALUE;
                break;
        }
        return result;
    }


    //判断输入的值是否为数字
    static String reg = "^\\d+(\\.\\d+)?$";

    public static boolean editTextNum(String num) {
        if (num.matches(reg)) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * @param detectionValue 检测值
     * @return
     */
    public static float getMsg(String detectionValue) {
        float result = 0;
        try {
            result = Float.parseFloat(detectionValue);
        } catch (Exception e) {

        }
        return result;
    }

    public static String Testingdetection(String reference_value,int Identification) {

        double maxValues = Double.MAX_VALUE;
        double minValues = -1;
        if (reference_value.contains("-")) {

            String[] reference = reference_value.split("-");

            if (UrlUtils.StrNum(reference[0]) && UrlUtils.StrNum(reference[1])) {

                minValues = Double.parseDouble(reference[0]);
                maxValues = Double.parseDouble(reference[1]);

            }


        } else if (reference_value.contains("~")) {

            String[] reference = reference_value.split("~");

            if (UrlUtils.StrNum(reference[0]) && UrlUtils.StrNum(reference[1])) {

                minValues = Double.parseDouble(reference[0]);
                maxValues = Double.parseDouble(reference[1]);


            }

        } else if (reference_value.contains("^"))

        {

            String[] reference = reference_value.split("^");

            if (UrlUtils.StrNum(reference[0]) && UrlUtils.StrNum(reference[1])) {

                minValues = Double.parseDouble(reference[0]);
                maxValues = Double.parseDouble(reference[1]);


            }

        } else if (reference_value.contains("<")) {

            String[] reference = reference_value.split("<");


            if (UrlUtils.StrNum(reference[1])) {

                maxValues = Double.parseDouble(reference[1]);


            }


        } else if (reference_value.contains("<="))

        {

            String[] reference = reference_value.split("<=");
            if (UrlUtils.StrNum(reference[1])) {
                maxValues = Double.parseDouble(reference[1]) + 0.00001;
            }


        } else if (reference_value.contains(">"))

        {


            String[] reference = reference_value.split(">");
            if (UrlUtils.StrNum(reference[1])) {
                minValues = Double.parseDouble(reference[1]);

            }

        } else if (reference_value.contains(">="))

        {

            String[] reference = reference_value.split(">=");
            if (UrlUtils.StrNum(reference[1])) {
                minValues = Double.parseDouble(reference[1]) - 0.00001;

            }

        }

        StringBuffer str=new StringBuffer();
        if(Identification==1){

            float minVal=Float.parseFloat(minValues+"");//将double转换成float来比较大小
            float maxVal=Float.parseFloat(maxValues+"");
            str.append(minVal);
            str.append("-"+maxVal);
        }else{
            float minVal=Float.parseFloat(minValues+"");
            float maxVal=Float.parseFloat(maxValues+"");
            str.append(minVal*0.1);
            str.append("-"+maxVal*10);
        }


        return str.toString();
    }



    //判断体温数值
    public static int TiWencrisisVal(double val) {

        int TemperatureSatats = 0;//0正常，1，低热，2高热

        if(35.7>val){
            TemperatureSatats=-1;
        }else if(35.7<=val && val <37.3){// }else if(36.0<=val && val <37.3){
            TemperatureSatats=0;
        }else if(37.3<=val && val <38.1){
            TemperatureSatats = 1;
        }else{
            TemperatureSatats = 2;
        }


        return TemperatureSatats;
    }

    //判断心率
    public static int hearRate(double val) {
        int stats = 0;
        if(val<59.9){
            stats=3;
        }else if(59.9<val && val<100.1){
            stats=0;
        }else{
            stats=4;
        }
        return stats;
    }


    /**
     * 血压判断
     */


    //收缩压
    public static int shrink(String value) {//ddd

        Float judgevalue = Float.parseFloat(value);

        if ( judgevalue < 80) {
            return 1;
        }
        if (judgevalue > 80.1 && judgevalue < 120.1) {
            return 2;
        }
        if (judgevalue > 120.1 && judgevalue < 139.1) {
            return 3;
        }
        if (judgevalue > 139.1 && judgevalue < 159.1) {
            return 4;
        }
        if (judgevalue > 159.1 && judgevalue < 179.1) {
            return 5;
        }
        if (judgevalue >179.1) {
            return 6;
        }

        return 0;
    }

    //舒张压
    public static int Diastole(String value) {//ppp
        Float judgevalue = Float.parseFloat(value);

        if ( judgevalue < 40.1) {
            return 1;
        }
        if (judgevalue > 40.1 && judgevalue < 80.1) {
            return 2;
        }
        if (judgevalue > 80.1 && judgevalue < 89.1) {
            return 3;
        }
        if (judgevalue > 89.1 && judgevalue < 99.1) {
            return 4;
        }
        if (judgevalue >99.1 && judgevalue <109.1) {
            return 5;
        }
        if (judgevalue >109.1) {
            return 6;
        }
        return 0;
    }

    public static int Valstats(String statsHigh, String statslow) {
        int stats = 0;

        if(shrink(statsHigh)>=4 && Diastole(statslow)<4){

            return 7;
        }
        if (shrink(statsHigh) > Diastole(statslow)) {
            stats = shrink(statsHigh);
        } else if (shrink(statsHigh) < Diastole(statslow)) {
            stats = Diastole(statslow);
        } else {
            stats = Diastole(statslow);
        }

        return stats;
    }


    //血糖
    //检测危急值 val检测值
    public static int bloodSugar(double val) {
        int stats = 0;//0正常1高血糖2危急值
        if (1.69 < val && val < 10.9) {//  if (2.79 < val && val < 10.9) {
            stats = 0;
        } else if(10.9<= val && val < 24.9) {
            stats = 1;
        }else{
            stats = 2;
        }
        return stats;
    }




}
